package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.CookieStore;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oracle.jrockit.jfr.RequestableEvent;

import dao.UserDAO;
import entity.User;

/**
 * Servlet implementation class UserLogin
 */
@WebServlet("/UserLogin")
public class UserLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDAO userDAO = UserDAO.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = (HttpSession)request.getSession(true);
		String user_id = (String)request.getParameter("userid");
		String user_password = request.getParameter("password");
		System.out.println("1111111111111111111"+user_id);
		Cookie cookies[] = request.getCookies();
		if(user_id==null&&user_password==null){//第一次从这里进
			if(cookies!=null){//只有cookie里面有东西才会让它返回
				System.out.println("没有输入，但是cookie里面都内容");
				for(int i=0;i<cookies.length;i++){
					System.out.println("cookie的第"+i+"项:"+cookies[i].getName()+":"+cookies[i].getValue());
					if(cookies[i].getName().equals("account")){
						request.setAttribute("account", cookies[i].getValue());
					}
					if(cookies[i].getName().equals("password")){
						request.setAttribute("password", cookies[i].getValue());
					}
				}
			}
			else{
				System.out.println("没有输入，cookie里面也没有内容");
			}
			request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
		}else if(user_id!=null&&user_password!=null){
			User user =userDAO.get(Integer.parseInt(user_id));
			if(user.getUser_password().equals(user_password))
			{//验证是正确的才会让他更新cookie.
				if(cookies!=null&&cookies.length>1){//不是空的就更新
					System.out.println("有输入，重新设置cookie");
					for(int i=0;i<cookies.length;i++){
						if(cookies[i].getName().equals("account")){
							cookies[i].setValue(user_id);
							response.addCookie(cookies[i]);
							//request.setAttribute("account", cookies[i].getValue());
						}
						if(cookies[i].getName().equals("password")){
							//request.setAttribute("password", cookies[i].getValue());
							cookies[i].setValue(user_password);
							response.addCookie(cookies[i]);
						}
					}
				}else{//空的就第一次设置一下
					System.out.println("没输入，第一次设置cookie");
					Cookie cookie1 = new Cookie("account", user_id);
					Cookie cookie2 = new Cookie("password", user_password);
					response.addCookie(cookie1);
					response.addCookie(cookie2);
					cookie1.setMaxAge(2*60);//2分钟
					cookie2.setMaxAge(2*60);
				}
				session.setAttribute("user", user);//后添加session
				//request.getRequestDispatcher("index.html").forward(request, response);
				response.sendRedirect("Index_Date");
			}
			else{//密码不匹配
				String isMatch = "false";
				response.sendRedirect("UserLogin.jsp?isMatch="+isMatch);
			}
		}
			//return;
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
